﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL_Class
{
    public class TacGia
    {
        NLLDataContext db;

        #region "Thêm mới tác giả"
        public string AddNewAuthor(Author ent)
        {
            db = new NLLDataContext();
            string result = string.Empty;
            try
            {
                db.Authors.InsertOnSubmit(ent);
                db.SubmitChanges();
                result = "Thêm mới thành công";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion

        #region "Sửa thông tin tác giả"
        public void UpdateAuthor(Author ent)
        {
            db = new NLLDataContext();
            try
            {
                Author au = db.Authors.Single(p => p.ID == ent.ID);
                au.AuthorName = ent.AuthorName;
                au.Description = ent.Description;

                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region "Xóa tác giả"
        public void DeleteAuthor(Author ent)
        {
            db = new NLLDataContext();
            try
            {
            var author = from p in db.Authors
                         where p.ID.Equals(ent.ID)
                         select p;

            db.Authors.DeleteAllOnSubmit(author);
            
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion
    }
}
